/**
 * @author helio
 */
if (!SALE.view) SALE.view = {}

if (!SALE.view.UploadImagePanel) {
	SALE.view.UploadImagePanel = Ext.extend(SALE.basicGridPanel, {
	
		layout: 'border',
		border: false,
		bodyStyle : 'background-color: transparent;', 
		initComponent: function(){
			
			this.onlineForm = new Ext.FormPanel({
				labelWidth: 60,
				border: false,
				buttonAlign: 'center',
				defaultType: 'textfield',
				bodyStyle : 'background-color: transparent;padding:10px;', 
												
				items: [{
					vtype: 'url',
					fieldLabel: '图片地址',
                    name: 'onlineurl',
                    anchor:'95%'		
				},{
					xtype: 'numberfield',
					fieldLabel: '图片宽度',
                    name: 'url',
                    anchor:'50%'		
				},{
					xtype: 'numberfield',
					fieldLabel: '图片高度',
                    name: 'url',
                    anchor:'50%'		
				}],
		      
				
				buttons: [{
		            text: '确定',
		            iconCls: 'picture',
		            handler: this.saveHandler.createDelegate(this)
		        }]
			});
			this.localForm = new Ext.FormPanel({
				labelWidth: 60,
				border: false,
				fileUpload: true,
				buttonAlign: 'center',
				defaultType: 'textfield',
				bodyStyle : 'background-color: transparent;padding:10px;', 
												
				items: [{
					xtype: 'fileuploadfield',
					fieldLabel: '图片地址',
                    name: 'UploadFile',
                    buttonText: '',
                    buttonCfg: {
                        iconCls: 'picture'
                    },
                    anchor:'95%'		
				},{
					xtype: 'numberfield',
					fieldLabel: '图片宽度',
                    name: 'url',
                    anchor:'50%'		
				},{
					xtype: 'numberfield',
					fieldLabel: '图片高度',
                    name: 'url',
                    anchor:'50%'		
				}],
		      
				
				buttons: [{
		            text: '上传',
		            iconCls: 'upload',
		            handler: this.uploadHandler.createDelegate(this)
		        }]
			});
			this.centerPanel = new Ext.TabPanel({
			    region: 'center',
			    deferredRender: false,
			    activeTab: 1,
			    items: [{
					title: '网络上的图片',
					autoScroll: true,
					bodyStyle: "padding: 10px",
					layout: 'fit',
					items: this.onlineForm
				}, {
			        title: '电脑里的图片',
			        autoScroll: true,
			        bodyStyle: "padding: 10px",
			        layout: 'fit',
			        items: this.localForm
			    }]
			});
			this.items = this.centerPanel;
			
			SALE.view.UploadImagePanel.superclass.initComponent.call(this);
		},
						
		uploadHandler : function() {
			if(this.localForm.form.isValid()) {
				this.localForm.form.doAction('submit', {
					url: SALE.App.configInfo.url + 'special/upload.php',
					waitMsg: '正在上传图片...',
					success: function(form, action){
						if(action.result && action.result.success) {
							SALE.view.UploadImagePanel.callback(action.result.file);
						} else {
							var file = form.findField("UploadFile").getValue();
							SALE.view.UploadImagePanel.callback(SALE.Utils.config.getFileUrl(file));
						}
                        SALE.view.popWin.hide();
                    },
					failure: function(form, action){
			        	this.showMessage(action.result.file);
			        }.createDelegate(this)
				});			
			}
		},
		
		saveHandler : function() {
			if(this.onlineForm.form.isValid()) {
				var url = this.onlineForm.form.findField("onlineurl").getValue();
				SALE.view.UploadImagePanel.callback(url);
                SALE.view.popWin.hide();
			}
		}
	});
	
	Ext.reg('uploadimagepanel', SALE.view.UploadImagePanel);
}

SALE.view.showUploadFileWin = function(callback) {
	SALE.view.UploadImagePanel.callback = callback;
	if(!SALE.view.popWin) {
		SALE.view.popWin = new Ext.Window({
		 		
		 	title: '图片',
			closable: true,	
			collapsible  : true,
			width: 400,
			height: 260,
			layout: 'fit',
			closeAction :'hide',
					
			items: {
				border: false,
				xtype : "uploadimagepanel"
			}
		});
	}
	
	SALE.view.popWin.show();
}